maori danii
maori danii

Reputation: 59

use my 1 thread on 2 or 4 cpu cores C# without parallel.for?

I have software (with very big loop) that must run in a certain order so i can not use multi-thread and i tried to work with parallel.for and it made a lot of problems because it does not work in right order. the only solution i'm thinking about is to use my 1 thread on 2 or 4 cpu cores. is it possible to run 1 thread on more then 1 cpu core with c#?

Upvotes: 0

Views: 624

Answers (3)

weston
weston

Reputation: 54781

There would be no benefit to using multiple cores executing in series.

However if you only need to maintain order of results, then look at ParallelEnumerable.AsOrdered.

Example:

var yourResults = yourList.AsParallel().AsOrdered().Select(DoProcessSingle);

DoProcessSingle could be called in any order, but the results it returns will be in the same order as if you did it in series, i.e.:

var yourResults = yourList.Select(DoProcessSingle);

Upvotes: 0

Benny Ae
Benny Ae

Reputation: 2016

this really depends on your cases.

In parallel programming they call "parallel able" or something like that.

If your following processes really depends on you previous result, You can NOT do it.

But maybe you can break it down in to some pieces.

Sorry about miss understanding your question.

For thread vs core. i will say , one core could go with many threads.

This really depends on your job type,

if your job is easy compute
but need to wait for some other things you could do multiple threads in one core without problem. E.g.
1, easy process data, 2, send data to another server 3, wait and get server responds. 4, easy process

if your job is heavy compute and may only working on your machine , even only on CPU.

then this may no be a good ideal for more thread.

but you can do tests to find out.

Again, as a mentioned, if you data/job really depends previous result, you can't do it in parallel.

Upvotes: -1

Christoph Fink
Christoph Fink

Reputation: 23093

Short answer. NO

Each thread has its own CPU/core to work on. If you want to work on 2+ CPUs/cores you need 2+ threads. A thread can switch cores if it e.g. is in waiting state an then resumes on another core, but cannot run on two cores at the same time...

If you show us what exactly you are trying to achieve we may be able help you, but as of now there is nothing more I could tell you.

Upvotes: 6

Related Questions