Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Pthreads with VC++ 6.0

Status
Not open for further replies.

bcbeyhan1

Programmer
Jan 31, 2005
3
TR
I need some help in using pthreads (win32 version) with vc++.
I have written some simple code to test the accuracy of pthreads in means of clock times or ticks then tried some combinations of sleep() function to make my program work in synch within the threads but nope it did not work or it works but with weird outputs which i will explain briefly here.
Following is my 2 simple threads one is a timer function which increases a variable called timer by one every cpu tick (by sleep(5)) the other is just used for printing out the value of timer variable when the value becomes 1 modulus 100. Here is the simple code :

unsigned int timer = 0;
void *tickThread(void *null)
{
for (;;)
{

if ((timer % 100) == 1)
{
printf("\n in tickThread and in if and Timer is : %u",timer);
}
}
return (void*)0;
}

void *timerThread(void *null)
{
for(;;)
{
Sleep(5);
timer++;
}
}
main ()
{
// create/initiate both threads
// join both threads
}

in main we first initiate both threads then we join both in the same time.

in this simple example we have a problem that is :

When both of the threads are initiated the variable timer is incrementing normaly but when we try to see the value of "timer" (our variable) from the other thread by using a conditional-if , some different problems may occur depending on the usage or place of sleep() function .

1-The problem with original code : Since the printf function is too slow to print the value of variable in the tickThread, the dummy cpu ignores the printf function and nothing is printed on to the screen. One thing is very important to note here that is ; the program executes and gets inside the if condinitional of tickThread normaly. Only the printf function is ommitted.
2-If we add Sleep(1) or Sleep(5) line to the if conditional of tickThread before the printf function then the output becomes even more bizarre that is : everytime when timer is equal to 1 modulus 100 the printf function in the if conditional of tickthread is executed but in a weird way. The program prints
a. in tickThread and in if and Timer is :101
in tickThread and in if and Timer is :102, if we had used Sleep(1) in the conditional or
b. in tickThread and in if and Timer is : 102 , if we’d used Sleep(5) at that line
3-The following (strange) output occurs if we add Sleep(5) line outside of and just before if condition in the infinite for loop of the thread function “tickThread”

in tickThread and in if and Timer is : 1
in tickThread and in if and Timer is : 101
in tickThread and in if and Timer is : 201
in tickThread and in if and Timer is : 301
in tickThread and in if and Timer is : 401
in tickThread and in if and Timer is : 501
in tickThread and in if and Timer is : 501
in tickThread and in if and Timer is : 601
in tickThread and in if and Timer is : 701
in tickThread and in if and Timer is : 801
in tickThread and in if and Timer is : 901
in tickThread and in if and Timer is : 1001
in tickThread and in if and Timer is : 1201
in tickThread and in if and Timer is : 1301
-----
and this erroneous output may change in every execution of program.

Ok you gurus out there ; come figure! :)
--------------------------------------------

"Advances are made by answering questions; discoveries are made by answering questions
 
Well that's thread programming for you.

If you're trying to syncronise two threads, then you need to use semaphores or mutexes. You can't just let two threads free-run (with or without sleeps) and expect any kind of consistency of results.



--
 
ok i have tried the mutexes in this way :

void *tickThread(void *null)
{
for (;;)
{
if ((timer % 100) == 1)
{
pthread_mutex_lock(&mutex);
Sleep(5);
printf("\n in tickThread and in if and Timer is : %u",timer);
pthread_mutex_unlock(&mutex);
}
}
return (void*)0;
}

void *timerThread(void *null)
{
for(;;)
{

Sleep(5);
pthread_mutex_lock(&mutex);
timer++;
pthread_mutex_unlock(&mutex);

}
}
(i wont mention about the main cause the main part of program only consists of initializing these theads and mutexes then return)
Well, now the program works ok BUT with some nasty discrepancies described below :
--> it seems that we need to rebuild it and use “go” (F5) to make the program work properly.
--> the related process of this program seems to get %99 of the CPU which needs to be overcame
--> after some time passed the output becomes wrong (e.g 1401 and 1402 may be printed on the screen !) most prolly because of the cpu usage which i have checked in task manager to be %99! yes %99 for this program's process
--> when we open another window or execute another app, our program acts (i.e timer increases) even slower and prints out irrelevant data to its console window. (my own opinon about the last 2 discrepancies is that we use Sleep() function and that causes all these weird things and we MUST find some other way to make the cpu wait for the related code segments to work in synch i think.




Advances are made by answering questions; discoveries are made by questioning answers.
 
Code:
if ((timer % 100) == 1)
isn't mutexed. Whenever you access a variable that's shared amongst thread you should apply some synch. mechanism.

/Per

www.perfnurt.se
 
>related process of this program seems to get %99 of the CPU

Yes, well the

Code:
  for (;;) 
    {    
      if ((timer % 100) == 1) 
      {
        //...
      }
    }
path executes constantly with a small sleep for every 100th timer tick only.

/Per

www.perfnurt.se
 
Well then umm what am i supposed to do to make this program work in the way i want? I really tried tons of combinations even read about all of the tutorials about the pthreads (oh and the tutorials also does not work correctly)
I want to printout the value of timer every 100millisecond by using multiple threads.
I want to know if my operating system does not allow such a multi threaded app to run which is coded with pthreads. My os is w2k.

Advances are made by answering questions; discoveries are made by questioning answers.
 
Not sure how pthreads differ from other threads (never use pthreads myself), but i'd have the timerThread signal every 100th tick and have the other thread just sit and wait for the signals (ie WaitForSingleObject).


/Per

www.perfnurt.se
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top