Thread synchronization problem

I

Ivan

Hi

I have following problem: I'm creating two threads who are performing some
tasks. When one thread finished I would like to restart her again (e.g. new
job). Following example demonstrates that. Problem is that when program is
started many threads are created (see output section), when only two should
be running at any time. Can you please help me to identify me where the
problem is?

Best regards
Ivan


Source
================

private void btnStartThreads_Click(object sender, System.EventArgs e)
{
JobAgent ag1 = new JobAgent ();
JobAgent ag2 = new JobAgent ();


while (true)
{
if (ag1.IsRunning == false)
{
Thread t1 = new Thread (new ThreadStart (ag1.Run));
t1.Start ();
}
if (ag2.IsRunning == false)
{
Thread t2 = new Thread (new ThreadStart (ag2.Run));
t2.Start ();
}

}
}
}

public class JobAgent
{
protected ThreadStatus status = new ThreadStatus ();

public void Run ()
{
IsRunning = true;
Debug.WriteLine ("Thread num. " + Convert.ToString
(Thread.CurrentThread.GetHashCode ()) + " started");
Thread.Sleep (5000);
Debug.WriteLine ("Thread num. " + Convert.ToString
(Thread.CurrentThread.GetHashCode ()) + " finished");
IsRunning = false;
}

public bool IsRunning
{
get
{
lock (status)
{
return status.isRunning;
}
}
set
{
lock (status)
{
status.isRunning = value;
}
}
}

}

public class ThreadStatus
{
public bool isRunning = false;
}
}
Output
================

Thread num. 10 started
Thread num. 13 started
Thread num. 14 started
Thread num. 15 started
Thread num. 16 started
Thread num. 17 started
Thread num. 18 started
Thread num. 58 started
Thread num. 11 started
Thread num. 12 started
Thread num. 19 started
Thread num. 20 started
Thread num. 21 started
Thread num. 22 started
Thread num. 23 started
Thread num. 24 started
Thread num. 57 started
Thread num. 25 started
Thread num. 26 started
Thread num. 27 started
Thread num. 28 started
Thread num. 10 finished
The thread '<No Name>' (0x1544) has exited with code 0 (0x0).
Thread num. 13 finished
The thread '<No Name>' (0x12dc) has exited with code 0 (0x0).
Thread num. 14 finished
The thread '<No Name>' (0xabc) has exited with code 0 (0x0).
Thread num. 15 finished
The thread '<No Name>' (0x930) has exited with code 0 (0x0).
Thread num. 17 finished
Thread num. 18 finished
The thread '<No Name>' (0xd7c) has exited with code 0 (0x0).
Thread num. 16 finished
The thread '<No Name>' (0xda8) has exited with code 0 (0x0).
Thread num. 29 started
The thread '<No Name>' (0x1590) has exited with code 0 (0x0).
 
J

Jon Skeet [C# MVP]

Ivan said:
I have following problem: I'm creating two threads who are performing some
tasks. When one thread finished I would like to restart her again (e.g. new
job). Following example demonstrates that. Problem is that when program is
started many threads are created (see output section), when only two should
be running at any time. Can you please help me to identify me where the
problem is?

Well for one thing, there's nothing to say that just because you've
called Start on the thread, the first line of the method you've
specified will have run.

I would suggest using events and the like to signal when a job has
finished, rather than relying on a status flag like this.
 
I

Ivan

I thought about events, but how can I achieve that only one thread per time
can fire that event (if two threads end at same time and try to fire e.g.
ThreadFinished event at the same time)?

Ivan
 
J

Jon Skeet [C# MVP]

Ivan said:
I thought about events, but how can I achieve that only one thread per time
can fire that event (if two threads end at same time and try to fire e.g.
ThreadFinished event at the same time)?

Well, you can use locking to make sure that only one of the threads is
executing the event handler at any one time...
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Ivan,

Even without events your code is ok. The problem is the moment you set the
IsRunning flag.
You cannot guarantee that the loop in the main thread wont run several times
before the first line of the worker thread set the falg. And you will endup
with many threads started. That's what actually happens.

to make your code work set the falg right before starting the thread

The loop should look like this

while (true)
{
if (ag1.IsRunning == false)
{
Thread t1 = new Thread (new ThreadStart (ag1.Run));
ag1.IsRunning = true;
t1.Start ();
}
if (ag2.IsRunning == false)
{
Thread t2 = new Thread (new ThreadStart (ag2.Run));
ag2.IsRunning = true;
t2.Start ();
}

}

remove setting the flag to true at the beginning of the Run method. Leave
setting the flag to false, though.

Run method should look like this.

public void Run ()
{
Console.WriteLine ("Thread num. " + Convert.ToString
(Thread.CurrentThread.GetHashCode ()) + " started");
Thread.Sleep (5000);
Console.WriteLine ("Thread num. " + Convert.ToString
(Thread.CurrentThread.GetHashCode ()) + " finished");
IsRunning = false;
}

Now ot will work correctly. However the main thread doesn't need to know
anything about setting that flag, as a matter of fact it doesn't need to
know that there are threads at all. So, my suggestion is to move the code
for starting the thread and setting the flag in the JobAgen class. The main
thread in this case will look as follows


while (true)
{
if (!ag1.IsBusy)
{
ag1.DoJob()
}
if (!ag2.IsBusy)
{
ag2.DoJob();
}

}

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Ivan said:
I thought about events, but how can I achieve that only one thread per time
can fire that event (if two threads end at same time and try to fire e.g.
ThreadFinished event at the same time)?

Ivan

(e.g.
program
 
I

Ivan

Thx. for help. I think that there is similar problem when thread ends: it
sets the flag that is not busy (last statement in thread), but thread will
be actualy terminated few milliseconds after. Is there some better way to
achieve same functionality like in example and to get complete
synchronization?

Ivan


Stoitcho Goutsev (100) said:
Hi Ivan,

Even without events your code is ok. The problem is the moment you set the
IsRunning flag.
You cannot guarantee that the loop in the main thread wont run several times
before the first line of the worker thread set the falg. And you will endup
with many threads started. That's what actually happens.

to make your code work set the falg right before starting the thread

The loop should look like this

while (true)
{
if (ag1.IsRunning == false)
{
Thread t1 = new Thread (new ThreadStart (ag1.Run));
ag1.IsRunning = true;
t1.Start ();
}
if (ag2.IsRunning == false)
{
Thread t2 = new Thread (new ThreadStart (ag2.Run));
ag2.IsRunning = true;
t2.Start ();
}

}

remove setting the flag to true at the beginning of the Run method. Leave
setting the flag to false, though.

Run method should look like this.

public void Run ()
{
Console.WriteLine ("Thread num. " + Convert.ToString
(Thread.CurrentThread.GetHashCode ()) + " started");
Thread.Sleep (5000);
Console.WriteLine ("Thread num. " + Convert.ToString
(Thread.CurrentThread.GetHashCode ()) + " finished");
IsRunning = false;
}

Now ot will work correctly. However the main thread doesn't need to know
anything about setting that flag, as a matter of fact it doesn't need to
know that there are threads at all. So, my suggestion is to move the code
for starting the thread and setting the flag in the JobAgen class. The main
thread in this case will look as follows


while (true)
{
if (!ag1.IsBusy)
{
ag1.DoJob()
}
if (!ag2.IsBusy)
{
ag2.DoJob();
}

}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Thx. for help. I think that there is similar problem when thread ends: it
sets the flag that is not busy (last statement in thread), but thread will
be actualy terminated few milliseconds after.

I don't see a problem there. The thread will end some time after, but since
it wont do any work you don't have to worry. Anyways next time you'll create
a new one
achieve same functionality like in example and to get complete
synchronization?

I presonally don't see anything wrong with that one. You shouldn't go too
complex for such simple sync scenario
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top