Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Bruno

threads are making me crazy

This topic is 5275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi.., I hope someone here has already done something with threads., if so, please help me. The problem is the following switch (opcao) { case ''0'' : break; case ''1'' : pthread_create (&thread, NULL,enviar, NULL); printf("\n bla bla bla , opcao 1"); printf("\n 11111111 bla bakl 111111 \n"); getchar();getchar(); break; } In that piece of code, i''m creathing a thread, wich has this function : I''m posting just the begining of it, so you guys can check: void enviar() { getchar(); fflush(stdin); printf("Please enter id number:"); scanf("%d",&IdDes); printf("\nPlease enter origin:"); scanf("%s",&origem); } The problem is that on screen, i only get printed "Please enter id number", and the entire rest of the function is ignored. However gcc, gives me this warnings : client.c:55: warning: passing arg 3 of `pthread_create'' from incompatible pointer type client.c:63: warning: passing arg 3 of `pthread_create'' from incompatible pointer type Anyone has any ideias, on how to fix this ? thanks, Bruno

Share this post


Link to post
Share on other sites
Advertisement
You did not wait for the thread to complete it's execution.What happens is: The thread is created, the execution of main continues and comes to an end. The created thread will be terminated no matter what it was doing (e.g: waiting for user input).
Look up the 'pthread_join' function to do that.
Sorry, I don't have much time now, will be back later. Here's an example from my operating systems class:

#include <pthread.h>
#include <stdio.h>

#define COUNT 100000


/* thread function */
void* thread_func(void *par)
{
/* get the current thread handle */
pthread_t self = pthread_self();
int i;
for (i = 0; i < COUNT; ++i)
{
printf("greetings from thread: %i, iteration: %i\n", (int)self, i);
}

return NULL;
}


int main()
{
/* we need 3 threads and just one attribute: will be the same for the 3 threads */
pthread_t thread[3];
pthread_attr_t thread_attr;

/* Initialise and set the scope of the attributes */
pthread_attr_init(&thread_attr);
pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);

/* create the threads */
pthread_create(thread, &thread_attr, thread_func, NULL);
pthread_create(thread+1, &thread_attr, thread_func, NULL);
pthread_create(thread+2, &thread_attr, thread_func, NULL);

/* and wait for them to finish their job */
pthread_join(thread[0], NULL);
pthread_join(thread[1], NULL);
pthread_join(thread[2], NULL);

/* cleanup to prevent memleaks */
pthread_attr_destroy(&thread_attr);

return 0;
}



Also: take a look at this website: http://www.planetoid.org/technical/pthreads/index.html


SwSh website!

[edited by - SwSh on June 6, 2004 1:09:59 PM]

[edited by - SwSh on June 6, 2004 1:11:02 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!