Exemplo n.º 1
0
void MultipleWriteTest(Thread& aCurrentThread, TUint aRPriority, TUint aW0Priority, TUint aW1Priority)
{
    const TUint writer0Iters = 300;
    const TUint writer1Iters = 500;
    FifoTest q(32);
    Writer0 writer0(q, aCurrentThread,  writer0Iters);
    Functor fWriter0 = MakeFunctor(writer0, &Writer0::Run);
    Writer1 writer1(q, aCurrentThread, writer1Iters);
    Functor fWriter1 = MakeFunctor(writer1, &Writer1::Run);
    Reader reader(q, aCurrentThread, writer0Iters + writer1Iters);
    Functor fReader = MakeFunctor(reader, &Reader::Run);

    ThreadFunctor* tReader0 = new ThreadFunctor("TSTA", fReader, aRPriority);
    ThreadFunctor* tWriter0 = new ThreadFunctor("TSTB", fWriter0, aW0Priority);
    ThreadFunctor* tWriter1 = new ThreadFunctor("TSTC", fWriter1, aW1Priority);
    tReader0->Start();
    tWriter0->Start();
    tWriter1->Start();

    aCurrentThread.Wait();
    aCurrentThread.Wait();
    aCurrentThread.Wait();

    // The worker threads should be ready to remove.
    delete tReader0;
    delete tWriter0;
    delete tWriter1;
}
Exemplo n.º 2
0
void MutexTutorial()
{
	/// .`Mutex`
	
	/// Mutex ("mutual exclusion") is a well known concept in multithreaded programming: When
	/// multiple threads write and read the same data, the access has to be serialized using
	/// Mutex. Following invalid code demonstrates why:
	
	Thread t;
	
	int sum = 0;
	t.Run([&sum] {
		for(int i = 0; i < 1000000; i++)
			sum++;
	});
	
	for(int i = 0; i < 1000000; i++)
		sum++;
	
	t.Wait();
	DUMP(sum);
	
	/// While the expected value is 2000000, produced value is different. The problem is that
	/// both thread read / modify / write `sum` value without any locking. Using `Mutex` locks
	/// the `sum` and thus serializes access to it - read / modify / write sequence  is now
	/// exclusive for the thread that has `Mutex` locked, this fixing the issue. `Mutex` can be
	/// locked / unlocked with `Enter` / `Leave` methods. Alternatively, `Mutex::Lock` helper
	/// class locks `Mutex` in constructor and unlocks it in destructor:
	
	Mutex m;
	sum = 0;
	t.Run([&sum, &m] {
		for(int i = 0; i < 1000000; i++) {
			m.Enter();
			sum++;
			m.Leave();
		}
	});
	
	for(int i = 0; i < 1000000; i++) {
		Mutex::Lock __(m); // Lock m till the end of scope
		sum++;
	}
	
	t.Wait();
	DUMP(sum);
	
	///
}
Exemplo n.º 3
0
void TestNetwork(const std::vector<Brn>& aArgs)
{
    OptionParser parser;
    OptionUint adapter("-i", "--interface", 0, "index of network adapter to use");
    parser.AddOption(&adapter);
    if (!parser.Parse(aArgs) || parser.HelpDisplayed()) {
        return;
    }

    std::vector<NetworkAdapter*>* ifs = Os::NetworkListAdapters(Net::InitialisationParams::ELoopbackUse, "TestNetwork");
    ASSERT(ifs->size() > 0 && adapter.Value() < ifs->size());
    TIpAddress addr = (*ifs)[adapter.Value()]->Address();
    for (TUint i=0; i<ifs->size(); i++) {
        (*ifs)[i]->RemoveRef("TestNetwork");
    }
    delete ifs;
    Endpoint endpt(0, addr);
    Endpoint::AddressBuf buf;
    endpt.AppendAddress(buf);
    Print("Using network interface %s\n\n", buf.Ptr());
    Thread* th = new MainNetworkTestThread(addr);
    th->Start();
    th->Wait();
    delete th;
}
Exemplo n.º 4
0
void TestThread()
{
    Thread* th = new MainTestThread();
    th->Start();
    th->Wait();
    delete th;
}
Exemplo n.º 5
0
void TestTimer(Environment& aEnv)
{
    Thread* th = new TimerTestThread(aEnv);
    th->Start();
    th->Wait();
    delete th;
}
Exemplo n.º 6
0
void TestThread()
{
    gTestStack = new TestStack();
    Thread* th = new MainTestThread();
    th->Start();
    th->Wait();
    delete th;
    delete gTestStack;
}