View topic - Question of threads

Question of threads

anything that doesn't fit to other groups.

Question of threads

Postby xuyong » Mon Jul 28, 2003 2:54 am

I have two threads,I want to thread2 running only when thread1 is idle,so I set the priority of thread1 to 41 and the priority of thread2 to 40,I make a test that set a special delay time to thread1 to simulate its exection time,when the time is more than 50ms,it's ok,but when the delay time is less than 50ms,sometimes thread2 has no chance to run,in my project the delay time of thread1 is only 2ms,how do I deal with the problem?
xuyong
Senior Member
 
Posts: 431
Joined: Sun Mar 02, 2003 7:52 am

Postby cdm » Mon Jul 28, 2003 5:16 pm

well, how are you testing to see if thread2 is running?
cdm
QNX Master
 
Posts: 789
Joined: Fri Jul 05, 2002 9:38 am

I use printf() to see if thread2 is running

Postby xuyong » Tue Jul 29, 2003 1:31 am

I use printf() to see if thread2 is running
xuyong
Senior Member
 
Posts: 431
Joined: Sun Mar 02, 2003 7:52 am

Postby cdm » Tue Jul 29, 2003 5:46 pm

Well, printf() does a lot of stuff. Not only formating/etc in libc, but also causes messages to be sent to the server managing your stdout/stderr file descriptors. Since server's float to the priority of the client (normally), that server will also fall to a lower priority then your thread1.

Also - generally using priorities between threads to force syncronization like this is not a good thing. It doesn't work on SMP and can lead to odd behaviors. A better thing to do is have a mutex or semaphore and have thread2 wait until thread1 says it can run and only run as long as that condition is true.
cdm
QNX Master
 
Posts: 789
Joined: Fri Jul 05, 2002 9:38 am

How to use mutex?

Postby xuyong » Wed Jul 30, 2003 12:51 am

I also want to use mutex,in my project, thread1 is resposible for getting data from dsp,thread2 is resposible for saving a large data to the file in emergency,thread2 is a contionus thread,its running should not effect running of thread1,if I use mutex,I have to use several times in thread2,but how to control the occation?what's your opinon? thank you!
xuyong
Senior Member
 
Posts: 431
Joined: Sun Mar 02, 2003 7:52 am

Postby cdm » Thu Jul 31, 2003 3:01 pm

In this case I would make an intelligent data structure using a reader/writer pattern that favors the writer. This means that thead1 will always be able to write data into the structure and that thread2 will be able to block on the structure until there is some amount X of data present. Look around online or in any textbooks on conncurent programming for explainations of reader/writer patterns.
cdm
QNX Master
 
Posts: 789
Joined: Fri Jul 05, 2002 9:38 am


Return to General Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron